﻿using System;
using System.Collections.Generic;
using Me.Prettyprint.Cassandra.Serializers;
using Me.Prettyprint.Cassandra.Service;
using Me.Prettyprint.Cassandra.Utils;
using Me.Prettyprint.Hector.Api;
using Me.Prettyprint.Hector.Api.Beans;
using Me.Prettyprint.Hector.Api.Factory;
using Me.Prettyprint.Hector.Api.Query;
using Org.Apache.Cassandra.Thrift;

namespace Me.Prettyprint.Cassandra.Model.Thrift
{
    /// <summary>
    /// Thrift implementation of the SuperColumnQuery
    /// </summary>
    /// <typeparam name="K"></typeparam>
    /// <typeparam name="SN"></typeparam>
    /// <typeparam name="N"></typeparam>
    /// <typeparam name="V"></typeparam>
    public class ThriftSuperColumnQuery<K, SN, N, V> : AbstractSuperColumnQuery<K, SN, N, V>, SuperColumnQuery<K, SN, N, V>
    {
        public ThriftSuperColumnQuery(Keyspace keyspace, Serializer<K> keySerializer,
            Serializer<SN> sNameSerializer, Serializer<N> nameSerializer, Serializer<V> valueSerializer) :
            base(keyspace, keySerializer, sNameSerializer, nameSerializer, valueSerializer)
        {
        }
        public override QueryResult<HSuperColumn<SN, N, V>> Execute()
        {
            if (ColumnFamilyName == null || SuperName == null)
            {
                throw new NullReferenceException("ColumnFamilyName and SuperName cannot be null");
            }
            Func<KeyspaceService, HSuperColumn<SN, N, V>> x = (KeyspaceService ks) =>
            {
                SuperColumn thriftSuperColumn = ks.GetSuperColumn(KeySerializer.ToByteBuffer(Key).GetBuffer(),
                    ThriftFactory.CreateSuperColumnPath(ColumnFamilyName, SuperName,
                    default(N), SuperNameSerializer, ColumnNameSerializer));
                return new HSuperColumnImpl<SN, N, V>(thriftSuperColumn, SuperNameSerializer, ColumnNameSerializer,
                    ValueSerializer);
            };
            return new QueryResultImpl<HSuperColumn<SN, N, V>>(
                Keyspace.DoExecute(new KeyspaceOperationCallbackBuilder<HSuperColumn<SN, N, V>>(x)), this);
        }
    }
}
